var ns, app;
app = new Vue({
    el: '#app',
    data: {
        ismoreshow: false,
        isviewhistory: false,
        isshow: false,
        form: {
            ID: 0,
            imagelist: [],
            processlist: [],

        },
        task_imgList: [],
        total: false,
        is_work_details: true,
        num: 6,
        showproess: false,
    },
    methods: {
        showProessAll: function () {
            var that = this;
            that.showproess = !that.showproess;
        },
        get_data: function() {
            var that = this;
            var options = {};
            options.ID = that.form.ID;
            options.action = "getqualitycontroldetail";
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    that.form = data.form;
                    that.task_imgList = data.form.imagelist;
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true
            });
        },
        img_browser: function(item) {
            var that = this;
            var imgurls = [];
            imgurls.push(item.imgurl);
            imageBrowser.openImages({
                imageUrls: imgurls,
                showList: false,
                activeIndex: 0
            });
        },
        tel_phone: function(item) {
            api.call({
                type: 'tel_prompt',
                number: item.phonenumber
            });
        },
        tel_phone2: function(phonenumber) {
            api.call({
                type: 'tel_prompt',
                number: phonenumber
            });
        },
        open_mask_more: function() {
            var that = this;
            that.ismoreshow = true;
        },
        hide_mask_more: function() {
            var that = this;
            that.ismoreshow = false;
        },
        openWin: function(item, params) {
            var that = this;
            if (item.url == '') {
                return;
            }
            params = params || {
                status: -1
            };
            ns.openWin(item.url, item.name, params);
        },
        do_canpublish: function() {
            var that = this;
           
            var options = {};
            options.ID = that.form.ID;
            options.action = "getqualitystate";
            ns.post(options, function (succeed, data, err) {
                if (data.status) {
                    if (data.ServiceStatus == that.form.ServiceStatus) {
                        ns.openWin('chooseserviceuser_frm', '选择负责人', {
                            source: 'chooseuser',
                            id: that.form.ID,
                            type: 1,
                            ServiceType: 3
                        });

                    }
                    else {
                        api.toast({
                            msg: "该工单状态已改变",
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                    that.get_data();
                    api.sendEvent({
                        name: 'reloadservicelist',
                    });
                } else {
                    api.toast({
                        msg: data.Msg,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        },
        //接单
        do_accpet_status:function(){
            var that = this;
            var options = {};
            options.ID = that.form.ID;
            options.action = "getqualitystate";
            ns.post(options, function(succeed, data, err) {
                if(data.status == true && data.ServiceStatus == that.form.ServiceStatus){
                    that.do_accpet();
                }else{
                    api.toast({
                        msg: "状态已改变，请重新选择",
                        duration: 2000,
                        location: 'bottom'
                    });
                    that.get_data();
                    api.sendEvent({
                        name: 'reloadservicelist',
                    });

                }
                
            })
        },
        do_accpet: function() {
            var that = this;
            var options = {};
            options.ID = that.form.ID;
            options.action = "acceptorderquality";
            ns.post(options, function(succeed, data, err) {
                if (succeed) {
                    if (data.status) {
                        api.toast({
                            msg: "操作成功",
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                    if (!data.status) {
                        api.toast({
                            msg: data.Msg,
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                    that.get_data();
                    api.sendEvent({
                        name: 'reloadservicelist',
                    });
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true,
                toastmsg: '提交中'
            });
        },
        //整改完成
        do_complete: function() {
            var that = this;
            var options = {};
            options.ID = that.form.ID;
            options.action = "getqualitystate";
            ns.post(options, function (succeed, data, err) {
                if (data.status == true && data.ServiceStatus == that.form.ServiceStatus) {
                    ns.openWin('qualitycontrolprocess_zhenggai', '整改完成', {
                        ID:that.form.ID,
                        ProjectID: that.form.ProjectID,
                        CheckUserID:that.form.InChargeUserIDList[0],
                        CheckUserName:that.form.InChargeUserNames,
                        type:1,//1-整改完成，2-验收
                    });
                } else {
                    api.toast({
                        msg: '状态已改变，请重新选择',
                        duration: 2000,
                        location: 'bottom'
                    });
                    that.get_data();
                    api.sendEvent({
                        name: 'reloadservicelist',
                    });
                }
            });
           
        },
        //验收
        do_yanshou:function(){
            var that = this;
            var options = {};
            options.ID = that.form.ID;
            options.action = "getqualitystate";
            ns.post(options, function (succeed, data, err) {
                if (data.status == true && data.ServiceStatus == that.form.ServiceStatus) {
                    ns.openWin('qualitycontrolprocess_yanshou', '整改验收', {
                        ID:that.form.ID,
                        ProjectID: that.form.ProjectID,
                        CheckUserID:that.form.InChargeUserIDs[0],
                        CheckUserName:that.form.InChargeUserNames,
                        type:2,//1-整改完成，2-验收
                    });
                } else {
                    api.toast({
                        msg: '状态已改变，请重新选择',
                        duration: 2000,
                        location: 'bottom'
                    });
                    that.get_data();
                    api.sendEvent({
                        name: 'reloadservicelist',
                    });
                }
            });
        },
        //催单
        doUrgeOrder: function () {
            var that = this;
            var options = {};
            options.ID = that.form.ID;
            options.action = "qualitycuidan";
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    if (data.status) {
                        api.toast({
                            msg:"操作成功",
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                    if (!data.status) {
                        api.toast({
                            msg: data.Msg,
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                    that.get_data();
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true,
                toastmsg: '提交中'
            });
        },
        //协同
        open_xietong: function() {
            var that = this;
            ns.openWin('chooseserviceuser_frm', '协同处理', {
                source: 'transfer',
                id: that.form.ID,
                type: 3,
                ServiceType: 3,
                is_xietong: true,
            });
        },
        do_returnback: function() {
            var that = this;
            var options = {};
            options.ID = that.form.ID;
            options.ServiceType = 2;
            options.action = "doreturnbackservice";
            ns.post(options, function(succeed, data, err) {
                if (succeed) {
                    that.get_data();
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true,
                toastmsg: '提交中'
            });
        },
        closebox:function(){
            var that=this;
            that.isshow=false;
        },
        //编辑
        do_edit:function(item){
            var that=this;
            ns.openWin('qualitycontrol_frm','修改工单',{
                ID:that.form.ID
            });
            that.isshow=false;

        },
        //删除
        do_delete:function(id){
            var that=this;
            that.isshow=false;
            api.confirm({
                title: '提示',
                msg: '确定删除当前任务吗',
                buttons: ['确定', '取消']
            }, function(ret, err) {
                if (ret.buttonIndex == 1) {
                    var options = {};
                    options.ID = id;
                    options.action = "doremovequalitycontrol";
                    ns.post(options, function(succeed, data, err) {
                        if (succeed) {
                            api.toast({
                                msg: '成功删除',
                                duration: 2000,
                                location: 'bottom'
                            });
                            api.sendEvent({
                                name: 'reloadservicelist',
                            });
                            // api.sendEvent({
                            //     name: 'updata_task_con',
                            // });
                            setTimeout(function () {
                                api.closeWin();
                            }, 500);
                        } else if (err) {
                            api.toast({
                                msg: err,
                                duration: 2000,
                                location: 'bottom'
                            });
                        }
                    });
                };
            });
        },
        //2-转交，4-重派
        do_chooseperson:function(id,type){
            ns.openWin('chooseserviceuser_frm','选择',{
                id:id,
                type:type,
                source:'chooseuser',
                ServiceType:3
            });
            that.isshow=false;

        },
    }
});
apiready = function() {
    api.parseTapmode();
    ns = window.Foresight.Util;
    imageBrowser = api.require('imageBrowser');
    app.form.ID = api.pageParam.id || 0;
    app.isviewhistory = api.pageParam.isviewhistory || false;
    app.get_data();
    if (!api.pageParam.is_work_details) {
        app.is_work_details = false;
    }
    api.addEventListener({
        name: 'updata_task_con'
    }, function(ret, err) {
        if (ret) {
            app.get_data();
            app.ismoreshow = false;
        } else {
            api.toast({
                msg: err,
                duration: 2000,
                location: 'bottom'
            });

        }
    });
    api.sendEvent({
        name: 'is_task_read',
        extra: {
            key1: app.form.ID,
        }
    });
    api.addEventListener({
        name: 'do_renwu_update'
    }, function (ret, err) {
        app.isshow=!app.isshow;

    });
    api.addEventListener({
        name: 'close_detail'
    }, function (ret, err) {
        api.closeWin();
    });
};
